package com.eduagent.xwqeduagent.controller;

import cn.hutool.core.util.ObjectUtil;
import com.eduagent.xwqeduagent.app.agent.GenerateMindMap;
import com.eduagent.xwqeduagent.app.agent.OnlineSearch;
import com.eduagent.xwqeduagent.app.agent.WebScraping;
import com.eduagent.xwqeduagent.app.teacher.MultimodalGeneration.MultiModGeneration;
import com.eduagent.xwqeduagent.common.BaseResponse;
import com.eduagent.xwqeduagent.common.ResultUtils;
import com.eduagent.xwqeduagent.exception.ErrorCode;
import com.eduagent.xwqeduagent.exception.ThrowUtils;
import com.eduagent.xwqeduagent.model.dto.MindMapDTO;
import com.eduagent.xwqeduagent.model.dto.OnlineSearchDTO;
import com.eduagent.xwqeduagent.model.dto.WebScrapingDTO;
import com.eduagent.xwqeduagent.model.vo.MindMapResultVO;
import com.eduagent.xwqeduagent.model.vo.OnlineSearchResultVO;
import com.eduagent.xwqeduagent.model.vo.WebScrapingResultVO;
import com.eduagent.xwqeduagent.service.ChatService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Slf4j
@RestController
@RequestMapping("/general")
public class GeneralController {

    @Resource
    private OnlineSearch onlineSearch;

    @Resource
    private WebScraping webScraping;

    @Resource
    private MultiModGeneration multiModGeneration;

    @Resource
    private GenerateMindMap generateMindMap;

    @Resource
    private ChatService chatService;

    /**
     * 联网搜索
     * @param onlineSearchDTO 请求体
     * @return BaseResponse<OnlineSearchResultVO>
     */
    @PostMapping("/OnlineSearch")
    public BaseResponse<OnlineSearchResultVO> OnlineSearch(@RequestBody OnlineSearchDTO onlineSearchDTO){

        String content = onlineSearchDTO.getContent();
        String userId = onlineSearchDTO.getUserId();
        String sessionId = onlineSearchDTO.getSessionId();

        ThrowUtils.throwIf(ObjectUtil.hasEmpty(content,userId,sessionId)
                , ErrorCode.NOT_FOUND_ERROR,"联网搜索请求参数不能为空");

        OnlineSearchResultVO onlineSearchResultVO = onlineSearch.performOnlineSearch(content, userId, sessionId);

        return ResultUtils.success(onlineSearchResultVO);
    }

    /**
     * 网页抓取
     * @param webScrapingDTO 抓取网页的请求体
     * @return BaseResponse<WebScrapingResultVO>
     */
    @PostMapping("/WebScraping")
    public BaseResponse<WebScrapingResultVO> WebScraping(@RequestBody WebScrapingDTO webScrapingDTO){

        String content = webScrapingDTO.getContent();
        String userId = webScrapingDTO.getUserId();
        String sessionId = webScrapingDTO.getSessionId();


        ThrowUtils.throwIf(ObjectUtil.hasEmpty(content,userId,sessionId)
                , ErrorCode.NOT_FOUND_ERROR,"网页抓取请求参数不能为空");

        WebScrapingResultVO webScrapingResultVO = webScraping.performWebScraping(content, userId, sessionId);

        return ResultUtils.success(webScrapingResultVO);
    }

    /**
     * 生成思维导图 - 原始方法保留，但可能会存在解析问题
     * @param mindMapDTO 请求体
     * @return BaseResponse<MindMapResultVO>
     */
    @PostMapping("/generateMindMap")
    public BaseResponse<MindMapResultVO> generateMindMap(@RequestBody MindMapDTO mindMapDTO) {
        String content = mindMapDTO.getContent();
        String userId = mindMapDTO.getUserId();
        String sessionId = mindMapDTO.getSessionId();

        ThrowUtils.throwIf(ObjectUtil.hasEmpty(content, userId, sessionId), 
                ErrorCode.NOT_FOUND_ERROR, "生成思维导图请求参数不能为空");

        MindMapResultVO mindMapResultVO = generateMindMap.generateMindMap(content, userId, sessionId);

        return ResultUtils.success(mindMapResultVO);
    }

    /**
     * 处理多模型流式聊天请求的端点方法
     * 该方法接收用户问题、会话ID和可选文件列表，返回流式文本响应
     *
     * @param question 用户输入的问题内容
     * @param sessionId 唯一会话标识符，用于关联聊天上下文
     * @param files 可选的上传文件列表，用于多模态处理（可为空）
     * @return Flux<String> 流式返回的文本响应，按事件流格式传输
     */
    @PostMapping(value = "stream-multi-model", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatStreamMultiModel(@RequestParam("question") String question,
                                             @RequestParam("sessionId") String sessionId,
                                             @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        return chatService.chatStreamMultiModel(question, sessionId, files);
    }

}
